package cn.tedu._11weibo.controller;

import cn.tedu._11weibo.base.response.JsonResult;
import cn.tedu._11weibo.base.response.StatusCode;
import cn.tedu._11weibo.mapper.CommentMapper;
import cn.tedu._11weibo.pojo.dto.CommentAddParam;
import cn.tedu._11weibo.pojo.entity.Comment;
import cn.tedu._11weibo.pojo.vo.UserVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Tag(name = "03-评论模块")
@Slf4j
@Validated
@RestController
@RequestMapping("/v1/comment/")
public class CommentController {
    @Autowired
    private CommentMapper commentMapper;

    /**
     * 添加评论功能
     */
    @PostMapping("insert")
    @Operation(summary = "添加评论")
    @ApiOperationSupport(order = 10)
    public JsonResult insert(@RequestBody CommentAddParam commentAddParam, HttpSession session){
        log.debug("commentAddParam = "+commentAddParam);
        UserVO userVO = (UserVO) session.getAttribute("user");
        if (userVO == null){
            return new JsonResult(StatusCode.NOT_LOGIN);
        }
        Comment comment = new Comment();
        BeanUtils.copyProperties(commentAddParam,comment);
        comment.setCreateTime(new Date());
        comment.setUserId(userVO.getId());
        commentMapper.insert(comment);
        return JsonResult.ok();
    }

    /**
     * 获取评论列表功能
     */
    @GetMapping("selectByWeiboId")
    @Operation(summary = "获取评论列表功能")
    @ApiOperationSupport(order = 20)
    public JsonResult list(Long weiboId) {
        log.debug("weiboId = "+weiboId);
        List<Comment> list = commentMapper.selectByWeiboId(weiboId);
        return JsonResult.ok(list);
    }
}
